package com.itheima.controller;

import com.itheima.utils.AliOSSProperties;
import com.itheima.entity.Result;
import com.itheima.utils.AliyunOSSUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

@RestController
public class UploadController {

    /*private final String bucketName = "java147-77"; // 存储空间名称
    private final String endpoint = "https://oss-cn-beijing.aliyuncs.com";  // 访问域名*/

    /*@Value("${aliyun.oss.bucketName}")
    private String bucketName ;
    @Value("${aliyun.oss.endpoint}")
    private String endpoint ;*/

    @Autowired
    private AliOSSProperties aliOSSProperties;

    /**
     * 文件上传,阿里云oss
     * @param file
     * @return
     */
    @PostMapping("/upload")
    public Result upload(MultipartFile  file) throws Exception {
        //1.获取文件名
        String orgiginalFilename = file.getOriginalFilename();
        String extName = orgiginalFilename.substring(orgiginalFilename.lastIndexOf("."));

        //2.上传文件
        String url = AliyunOSSUtils.upload(aliOSSProperties.getEndpoint(), aliOSSProperties.getBucket(), file.getBytes(), extName);

        //3.返回结果
        return Result.success(url);
    }

}
